<template>

    <el-dialog
        :visible.sync="dialogVisible"
        title="发货"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="快递公司" prop="expressName">
                <el-select v-model="formData.expressName" filterable placeholder="请选择" style="width:200px;">
                    <el-option
                        v-for="item in expressList"
                        :key="item.expressName"
                        :label="item.expressName"
                        :value="item.expressName"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="快递单号" prop="expressNo">
                <el-input v-model="formData.expressNo"/>
            </el-form-item>
            <el-form-item label="快递费" prop="expressMoney">
                <el-input type="number" v-model="formData.expressMoney"/>
            </el-form-item>
            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {OrderApi} from '@/api/mall/order'
import {ExpressApi} from '@/api/mall/express'

export default {
    name: 'MallOrderShipments',
    components: {},
    data() {
        return {
            formData: {
                orderNo: '',
                expressName: '',
                expressNo: '',
                expressMoney: 0
            },
            rules: {
                expressName: [
                    {required: true, message: '请选择快递公司', trigger: 'blur'}
                ],
                expressNo: [
                    {required: true, message: '请输入快递单号', trigger: 'blur'},
                    {min: 10, max: 60, message: '长度在 10 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            expressList: []
        }
    },
    methods: {
        // 显示组件
        showUpdate(orderNo) {
            this.dialogVisible = true
            this.formData.orderNo = orderNo
            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }
            this.getExpressList()
        },

        getExpressList() {
            ExpressApi.getAllList().then((response) => {
                this.expressList = response
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    let data = Object.assign({}, that.formData)
                    data.expressMoney = data.expressMoney * 100
                    OrderApi.shipments(data).then((response) => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>

<style>

</style>
